package com.liao.furns.web; /**
 * @author lzp
 * @version 1.0
 * lzpnb!
 */

import com.google.gson.Gson;
import com.liao.furns.pojo.Member;
import com.liao.furns.service.MemberService;
import com.liao.furns.service.impl.MemberServiceImpl;
import com.google.code.kaptcha.*;
import com.sun.xml.internal.bind.v2.model.core.ID;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import static com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY;

@WebServlet(name = "memberServlet", value = "/memberServlet")
public class memberServlet extends BasicServlet{
    MemberService memberService = new MemberServiceImpl();

//    @Override
//    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
////        System.out.println("memberServlet is called");
//        String  action = request.getParameter("action");
//        if("login".equals(action)){
//            login(request,response);
//        }else if ("register".equals(action)){
//            register(request,response);
//        }
//    }
    public void isExistUserName(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        boolean isExistUsername = memberService.isExistUsername(username);
        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("isExist",isExistUsername);
        String resultJson = new Gson().toJson(resultMap);
//        String resultJson = "{\"isExist\":"+ isExistUsername + "}";
        response.getWriter().write(resultJson);
    }

    public void isExistCode(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String code = request.getParameter("code");
        Map<String,Object> resultMap = new HashMap<>();
        String token = (String)request.getSession().getAttribute(KAPTCHA_SESSION_KEY);
        request.getSession().removeAttribute(KAPTCHA_SESSION_KEY);
        if(token != null && token.equalsIgnoreCase(code)) {
            resultMap.put("isExist",true);
        }else {
            resultMap.put("isExist",false);
        }
        String resultJson = new Gson().toJson(resultMap);
        response.getWriter().write(resultJson);
    }

    public void login(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        System.out.println("login is called");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        Member member = new Member(null, username, password, null);
        Member member1 = memberService.login(member);
        if(member1 != null){
            HttpSession session = request.getSession();
            session.setAttribute("name",username);
            session.setAttribute("member", member1);
            request.getRequestDispatcher("/views/member/login_ok.jsp").forward(request,response);
        }else {
            request.setAttribute("username",member.getUsername());
            request.setAttribute("nameorpwderror","用户名或密码错误");
            request.getRequestDispatcher("/views/member/login.jsp").forward(request,response);
        }
    }

    public void logout(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        if (session != null) {
            session.invalidate();
        }
        response.sendRedirect(request.getContextPath() + "/index.jsp");
    }

    public void register(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String email = request.getParameter("email");
        String code = request.getParameter("code");
        String token = (String)request.getSession().getAttribute(KAPTCHA_SESSION_KEY);
        request.getSession().removeAttribute(KAPTCHA_SESSION_KEY);
        if(token != null && token.equalsIgnoreCase(code)){
            if(!memberService.isExistUsername(username)){
                Member member = new Member(null, username, password, email);
                if(memberService.registerMember(member)){
                    request.getRequestDispatcher("/views/member/register_ok.jsp").forward(request,response);
                }else {
                    request.getRequestDispatcher("/views/member/register_fail.jsp").forward(request,response);
                }
            }else {
                request.getRequestDispatcher("/views/member/login.jsp").forward(request,response);
            }
        }else {
            request.setAttribute("msg","验证码不正确");
            request.setAttribute("active","register");
            request.setAttribute("username",username);
            request.setAttribute("email",email);
            request.getRequestDispatcher("/views/member/login.jsp").forward(request,response);
        }

    }
}
